﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKCodeBase.WinForm;
using YRevitJig;
using RevitInteractiveUtility.ExternalEventManager;

namespace YJKDimensionModel.PlaneSlopeDimension
{
	[Transaction(TransactionMode.Manual)]
	internal class TestCommand : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Selection selection = activeUIDocument.Selection;
			SlopeDimensionStructCol slopeDimensionStructCol = new SlopeDimensionStructCol(document);
			slopeDimensionStructCol.Init();
			if (TestCommand._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				TestCommand._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			SlopeDimensionForm slopeDimensionForm = new SlopeDimensionForm(slopeDimensionStructCol, TestCommand._hWndRevit);
			slopeDimensionForm.Show(TestCommand._hWndRevit);
			int wMsg = 7;
			TestCommand.SendMessage(TestCommand._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			try
			{
				for (;;)
				{
					XYZ xyz = selection.PickPoint("请选择坡度标注第一点");
					XLineJig xlineJig = new XLineJig(activeUIDocument);
					XYZ endPoint = xlineJig.PickEndPoint(xyz, "请选择坡度标注第二点");
					SlopeDimensionStruct slopeDimensionStruct;
					if (this.GetSlopeDimStructByName(slopeDimensionForm.ArrowType, slopeDimensionStructCol, out slopeDimensionStruct))
					{
						slopeDimensionStruct.UpText = slopeDimensionForm.LineUpText;
						slopeDimensionStruct.DownText = slopeDimensionForm.LineDownText;
						slopeDimensionStruct.Font = slopeDimensionForm.FontType;
						slopeDimensionStruct.TextHeight = double.Parse(slopeDimensionForm.TextHeight);
						slopeDimensionStruct.TextWidth = double.Parse(slopeDimensionForm.TextWidth);
						slopeDimensionStruct.ArrowAngle = double.Parse(slopeDimensionForm.ArrowAngle);
						slopeDimensionStruct.ArrowLength = double.Parse(slopeDimensionForm.ArrowLength);
						slopeDimensionStruct.Offset = double.Parse(slopeDimensionForm.Offset);
						slopeDimensionStruct.Length = this.GetLength(slopeDimensionForm.LineUpText, slopeDimensionForm.LineDownText);
						SlopeDimensionData slopeDimData = new SlopeDimensionData(commandData, slopeDimensionStruct, xyz, endPoint);
						SlopeDimesionOper.AddSlopeDimToDoc(slopeDimData);
						if (slopeDimensionForm.IsDisposed)
						{
							break;
						}
					}
				}
				return this.Execute(commandData, ref message, elements);
			}
			catch (Exception)
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void InitExternalEventRequest()
		{
			ExternalEventManagerSingleton eventManger = ExternalEventManagerSingleton.GetEventManger(false);
			int iRequestId = 1;
			AddSlopeDimensionRequest appendValue = new AddSlopeDimensionRequest(iRequestId);
			eventManger.AppendRequest(appendValue);
		}

		private double GetLength(string name1, string name2)
		{
			double num = SlopeDimesionOper.ReadTextMaxLenth(name1, name2);
			int num2 = (int)Math.Ceiling((num - 6.0) / 2.0);
			return (double)(8 + num2);
		}

		private bool GetSlopeDimStructByName(string name, SlopeDimensionStructCol slopeDimStructCol, out SlopeDimensionStruct slopeDimStruct)
		{
			if (slopeDimStructCol.LstSlopeDimensionStruct.Count < 0)
			{
				slopeDimStruct = null;
				return false;
			}
			foreach (SlopeDimensionStruct slopeDimensionStruct in slopeDimStructCol.LstSlopeDimensionStruct)
			{
				if (slopeDimensionStruct.Name == name)
				{
					slopeDimStruct = slopeDimensionStruct;
					return true;
				}
			}
			slopeDimStruct = null;
			return false;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private static WindowHandle _hWndRevit;

		private enum ERequestId
		{
			eAddSlopeDim = 1,
			eNoOper
		}
	}
}
